package com.hurst.autotap;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHidDevice;
import android.content.Context;
import android.os.Handler;

import java.util.Queue;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentLinkedQueue;

public class HidConsts {

    public static final String TAG = "u-HidConsts";

    public final static String NAME = "AutoTap";
    public final static String DESCRIPTION = "fac";
    public final static String PROVIDER = "funny";
    public static BluetoothHidDevice HidDevice;
    public static BluetoothDevice BtDevice;
    private static Handler handler;
    private static final Queue<HidReport> inputReportQueue = new ConcurrentLinkedQueue<>();

    protected static void addInputReport(final HidReport inputReport) {
        if (inputReport != null) {
            inputReportQueue.offer(inputReport);
        }
    }
    //具体参考https://blog.csdn.net/hurst2011/article/details/115732779
    public final static byte[] Descriptor  = new byte[]{};

    static long scheperoid = 5;
    public static void reporttrans(Context context){
        handler = new Handler(context.getMainLooper());
        new Timer().scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                HidReport report = inputReportQueue.poll();
                if(report != null){
                    if (HidUitls.IsConnected()) {
                        postReport(report);
                    }
                }
            }
        }, 0, scheperoid);
    }

    public static void postReport(HidReport report){
        report.SendState = HidReport.State.Sending;
        boolean ret = HidDevice.sendReport(BtDevice,report.ReportId,report.ReportData);
        if(!ret){
            report.SendState = HidReport.State.Failded;
        }else{
            report.SendState = HidReport.State.Sended;
        }
    }

    private static HidReport MTouchReport = new HidReport(HidReport.DeviceType.Mouse,(byte)0x01,new byte[]{0,0,0,0,0,0});

    public static void MTouch_Move_Android(int touchid, int x1, int y1){
        if(MTouchReport.SendState.equals(HidReport.State.Sending)){
            //return;
        }
        HidReport MTouchReport0 = new HidReport(HidReport.DeviceType.Mouse,(byte)0x01,new byte[]{0,0,0,0,0,0});
        MTouchReport0.ReportData[0] = 1;
        MTouchReport0.ReportData[1] = (byte) touchid;
        MTouchReport0.ReportData[3] = (byte) ((x1 >>> 8) & 0xFF);
        MTouchReport0.ReportData[2] = (byte) (x1 & 0xFF);
        MTouchReport0.ReportData[5] = (byte) (y1 >>> 8);
        MTouchReport0.ReportData[4] = (byte) (y1 & 0xFF);

        addInputReport(MTouchReport0);
    }

    public static void MTouch_Reset(int touchid){
        HidReport ReportData0 = new HidReport(HidReport.DeviceType.Mouse,(byte)0x01,new byte[]{0,0,0,0,0,0});
        ReportData0.ReportData[0] = 0;
        ReportData0.ReportData[1] = (byte) touchid;
        addInputReport(ReportData0);
    }

}
